package cn.otrue.patienthealthmanager.view;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.ScatterChart;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.model.XYValueSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import cn.otrue.patienthealthmanager.utils.CommonUtil;

/**
 * 负责数据图表界面两种图表的生成 血糖 血压和心率
 * 
 */
public class ChartViewProdutor extends AbstractDemoChart {
	private Context context;

	public ChartViewProdutor(Context context) {
		this.context = context;
	}

	/**
	 * 生成血糖(折线)图表
	 * 
	 * @param values
	 *            每天的血糖值
	 * @param dates
	 *            要显示的日期列表
	 * @return 血糖图表
	 */
	public GraphicalView getBloodChart(List<double[]> values, List<String> dates) {
		// 初始化坐标点的样式和颜色
		// 橙 红 黑 蓝
		int[][] colors = new int[][] {new int[] { Color.parseColor("#FFAA25"),Color.parseColor("#FFFF00") },new int[] { Color.parseColor("#000000"),Color.parseColor("#0016E0") } };
		PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,PointStyle.CIRCLE };
		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
		// 添加数据异常时显示的红色序列
		XYSeriesRenderer abnormalRenderer = new XYSeriesRenderer();
		abnormalRenderer.setColor(Color.parseColor("#ac371f"));
		abnormalRenderer.setPointStyle(PointStyle.CIRCLE);
		
		XYSeriesRenderer abnormalRender = new XYSeriesRenderer();//绿色线
		abnormalRender.setColor(Color.parseColor("#00ff00"));
		abnormalRender.setPointStyle(PointStyle.CIRCLE);
		
		// 餐前与餐后分别对应一个异常序列
		renderer.addSeriesRenderer(0, abnormalRenderer);
		renderer.addSeriesRenderer(1, abnormalRender);
		
		int length = renderer.getSeriesRendererCount();
		for (int i = 0; i < length; i++) {
			((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
		}
		// 分别设置X/Y轴显示的点数
		renderer.setXLabels(0);
		renderer.setYLabels(7);
		double[] x = new double[dates.size()];
		double[] x2 = new double[dates.size()];
		double xMark = 1;
		for (int i = 0; i < dates.size(); i++) {
			if (values.get(0)[i] > 0 && values.get(1)[i] > 0) {
				x[i] = xMark;
				x2[i] = xMark;
				renderer.addXTextLabel(xMark, dates.get(i));
				xMark++;
			} else if (values.get(0)[i] > 0) {
				x[i] = xMark;
				renderer.addXTextLabel(xMark, dates.get(i));
				renderer.getSeriesRendererAt(0).setColor(Color.RED);
				xMark++;
			} else if (values.get(1)[i] > 0) {
				x2[i] = xMark;
				renderer.addXTextLabel(xMark, dates.get(i));
				xMark++;
			}
		}
		// 分别设置X/Y轴缩放、移动时的范围
		if (xMark > 6)
			renderer.setPanLimits(new double[] { 0, xMark, 0, 0 });
		else
			renderer.setPanLimits(new double[] { 0, 0, 0, 0 });
		renderer.setZoomEnabled(false, false);
		renderer.setPanEnabled(true, false);
		List<double[]> xValues = new ArrayList<double[]>();
		xValues.add(x);
		xValues.add(x2);

		double maxValue_after = CommonUtil.getMaxValue(values.get(1));
		double minValue_after = CommonUtil.getMinValue(values.get(1));
		double minValue_before = CommonUtil.getMinValue(values.get(0));
		double maxValue_before = CommonUtil.getMaxValue(values.get(0));
		double minValue = CommonUtil.getMinValue(new double[] { maxValue_after,minValue_after, minValue_before, maxValue_before });
		double maxValue = CommonUtil.getMaxValue(new double[] { maxValue_after,minValue_after, minValue_before, maxValue_before });

		setChartSettings(renderer, "", "", "", 0.9, 6.9, minValue * 0.9,maxValue * 1.1, Color.GREEN);
		XYMultipleSeriesDataset dataset = buildDataset(new String[] { "", "", }, xValues, values);
		// 添加餐前异常序列的DataSet(value的顺序：餐前、餐后、餐前异常、餐后异常 。xValues顺序：餐前对应的x，餐后对应的x)
		XYSeries beAbnormalSeries = new XYSeries("", 0);
		double[] xV = xValues.get(0);
		double[] yV = values.get(0);
		double[] isNormal = values.get(0);
		int seriesLength = xV.length;
		for (int k = 0; k < seriesLength; k++) {
			if (isNormal[k] > 0)
				beAbnormalSeries.add(xV[k], yV[k]);
		}
		dataset.addSeries(2, beAbnormalSeries);

		// 添加餐后异常序列的DataSet(value的顺序：餐前、餐后、餐前异常、餐后异常 。xValues顺序：餐前对应的x，餐后对应的x)
		XYSeries afAbnormalSeries = new XYSeries("", 0);
		double[] xV2 = xValues.get(1);
		double[] yV2 = values.get(1);
		double[] isNormal2 = values.get(1);
		int seriesLength2 = xV.length;
		for (int k = 0; k < seriesLength2; k++) {
			if (isNormal2[k] > 0)
				beAbnormalSeries.add(xV2[k], yV2[k]);
		}
		dataset.addSeries(3, afAbnormalSeries);

		String[] types = new String[] { LineChart.TYPE, LineChart.TYPE,
				ScatterChart.TYPE, ScatterChart.TYPE };

		return ChartFactory.getCombinedXYChartView(context, dataset, renderer,
				types);
	}

	/**
	 * 生成心率(折线图)+血压(高低柱)图
	 * 
	 * @param values
	 *            心率,高压，低压
	 * @param dates
	 *            日期
	 * @return
	 */
	public GraphicalView getPlusPressureChart(List<double[]> allValues,List<String> dates) {
		
		List<double[]> x = new ArrayList<double[]>();
		double[] xList = new double[dates.size()];
		for (int i = 0; i < dates.size(); i++) {
			xList[i] = (i + 1);
		}
		x.add(xList);

		List<double[]> values = new ArrayList<double[]>();
		values.add(allValues.get(0));
//		values.add(allValues.get(1));
		
		int[] colors = new int[] { Color.parseColor("#86a3b5") };
		PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE };

		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
		renderer.setPointSize(8f);

		renderer.setDisplayChartValues(true);// 将点的值显示出来
		renderer.setChartValuesTextSize(25);// 点的值的文字大小
		int length = renderer.getSeriesRendererCount();
		for (int i = 0; i < length; i++) {
			XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
			// 设置是否是实心点
			r.setFillPoints(true);
		}

		
		//获取y轴刻度
		double maxValue1 = CommonUtil.getMaxValue(allValues.get(0));//心率最高值
		double maxValue2 = CommonUtil.getMaxValue(allValues.get(1));//高血压中的最高值
		double maxValue3 = CommonUtil.getMaxValue(allValues.get(2));//高血压中的最低值

		double minValue1 = CommonUtil.getMinValue(allValues.get(0));//心率最低值
		double minValue2 = CommonUtil.getMinValue(allValues.get(1));//高血压中的最低值
		double minValue3 = CommonUtil.getMinValue(allValues.get(2));//低血压中的最低值

		double maxValue = CommonUtil.getMaxValue(new double[] { maxValue1,maxValue2, maxValue3 });//三个最高值中的最高值设置为最高的y值
		double minValue = CommonUtil.getMinValue(new double[] { minValue1,minValue2, minValue3 });//三个最低值中的最低值设置为最低的y值

		
		
		
		
		setChartSettings(renderer, "", "", "", 0.5, 6.9, minValue * 0.9,maxValue * 1.1, Color.parseColor("#86a3b5"));
		renderer.setXLabels(0);
		renderer.setYLabels(0);
		renderer.setYAxisMin(0);// y轴的起始坐标
		// 设置x，y轴是否能滑动
		renderer.setPanEnabled(true, false);
		// 设置x，y轴是否能放大缩小
		renderer.setZoomEnabled(false, false);
		// 设置标题的字体大小
		renderer.setChartTitleTextSize(25);
		for (int i = 0; i < dates.size(); i++) {
			// 设置X轴对应的文字
			renderer.addXTextLabel(i + 1, dates.get(i));
		}
		// 设置y轴对应的坐标个数及显示文字
		renderer.addYTextLabel(120, "120");
		renderer.addYTextLabel(80, "80");
		renderer.addYTextLabel(60, "60");
		renderer.addYTextLabel(40, "40");
		renderer.addYTextLabel(0, "0");
		// 设置是否显示网格
		renderer.setShowGrid(true);
		renderer.setLegendTextSize(15);
	
		renderer.setXLabelsAlign(Align.CENTER);	// x轴点和文字的对应关系：这是坐标值与刻度的对应关系
		renderer.setYLabelsAlign(Align.CENTER);// y轴点和文字的对应关系：这是坐标值与刻度的对应关系
		// 设置X,Y轴单位的字体大小
		renderer.setLabelsTextSize(30);
		// 点的间隔
		renderer.setBarSpacing(1);//renderer.setBarSpacing(0.5f);// 柱形图间隔
		// 设置滑动范围
		if (dates.size() > 6)
			renderer.setPanLimits(new double[] { 0, dates.size() + 1, 0, 0 });
		else
			renderer.setPanLimits(new double[] { 0, 0, 0, 0 });

		
		
		//添加一个柱状图
		XYSeries hSeries = new XYSeries("");
		for (int i = 0; i < allValues.get(1).length; i++) {
			hSeries.add((i + 1), allValues.get(1)[i]);
		}
		XYSeriesRenderer hRenderer = new XYSeriesRenderer();
		hRenderer.setColor(Color.RED);

		XYSeries hSer = new XYSeries("");
		for (int i = 0; i < allValues.get(2).length; i++) {
			hSer.add((i + 1), allValues.get(2)[i]);
		}
		XYSeriesRenderer hRe = new XYSeriesRenderer();
		hRe.setColor(Color.BLUE);

		
		XYSeries lSer = new XYValueSeries("");
		for (int i = 0; i < allValues.get(3).length; i++) {
			lSer.add((i + 1), allValues.get(3)[i]);
		}
		XYSeriesRenderer lRend = new XYSeriesRenderer();
		lRend.setColor(Color.YELLOW);
		
		XYSeries lSeries = new XYValueSeries("");
		for (int i = 0; i < allValues.get(4).length; i++) {
			lSeries.add((i + 1), allValues.get(4)[i]);
		}
		XYSeriesRenderer lRenderer = new XYSeriesRenderer();
		lRenderer.setColor(Color.WHITE);
		
		String[] titles = new String[] { "" };
		XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);//
		dataset.addSeries(0, lSeries);
		dataset.addSeries(0, lSer);
		dataset.addSeries(0, hSer);
		dataset.addSeries(0, hSeries);
		
		renderer.addSeriesRenderer(0, lRenderer);
		renderer.addSeriesRenderer(0, lRend);
		renderer.addSeriesRenderer(0, hRe);
		renderer.addSeriesRenderer(0, hRenderer);
		
		lRenderer.setChartValuesTextSize(30f);
		lRenderer.setDisplayChartValues(true);
		
		hRenderer.setChartValuesTextSize(30f);
		hRenderer.setDisplayChartValues(true);
		// BarChart柱状图 LineChart折线图
		String[] types = new String[] { BarChart.TYPE, BarChart.TYPE,BarChart.TYPE,BarChart.TYPE,LineChart.TYPE };
		return ChartFactory.getCombinedXYChartView(context, dataset, renderer,types);

	}
}
